﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SQLite;

public partial class SaleInfo_UnitDay : UDCPage
{
	private int _did = -1;
    protected int DID {
        get { return _did; }
        set { _did = value; }
    }
	
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            DID = String.IsNullOrEmpty(Request.QueryString["Did"]) 
                ? GetDid() : Int32.Parse(Request.QueryString["Did"].ToString());
            SaleInfoDataBind();
            if (IsReported())
            {
                divfooter.Visible = false;                                              
            }
            else
            {
                divfooter.Visible = true;
                if ((!String.IsNullOrEmpty(Request.QueryString["msg"]))
                && Request.QueryString["msg"].ToString().Equals("completed", StringComparison.InvariantCultureIgnoreCase))
                {
                    CountReported();
                    divfooter.Visible = false;
                }  
                if (saleCount.Text.Equals("0") && !(!String.IsNullOrEmpty(Request.QueryString["msg"])
                    && Request.QueryString["msg"].ToString().Equals("NoFirst"
                        , StringComparison.InvariantCultureIgnoreCase)))
                {
                    Response.Redirect("NewSale.aspx");
                }
            }
        }        
    }

    private void SaleInfoDataBind()
    {
        string sqlstr = @"select sid,ServiceId,case isnew when 1 then '新用户' else '老用户' end as isnew
                    ,case iscontract when 1 then '预存话费送手机' else '购机入网送话费' end as iscontract
                    ,saledesc,t3.dvname from ud_saleinfo_t t1 
                    inner join ud_developer_t t3 on t1.DvId = t3.DvId and t3.Did = :Did and t3.ifvalid = 1
                    where date(saledate) = date('now','localtime')";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter(":Did",DbType.Int32)
                                };
        spc[0].Value = DID;
        DataSet ds = SQLiteHelper.ExecuteDataset(sqlstr, spc);
        UnitDaySales.DataSource = ds;
        UnitDaySales.DataBind();
        saleCount.Text = ds.Tables[0].Rows.Count.ToString();
    }

    private void CountReported()
    {
        string sqlstr = "insert into ud_reported_t values(:did,:repdate)";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter(":did",DbType.Int32),
                                    new SQLiteParameter(":repdate", DbType.DateTime)
                                };
        spc[0].Value = GetDid();
        spc[1].Value = System.DateTime.Now;
        SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
    }

    private bool IsReported()
    {
        string sqlstr = "select count(1) from ud_reported_t where did = :did and date(repdate) = date('now','localtime')";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter(":did",DbType.Int32)
                                };
        spc[0].Value = DID;
        int count = 0;
        Int32.TryParse(SQLiteHelper.ExecuteScalar(sqlstr, spc).ToString(), out count);
        return count > 0;
    }
}